package net.dgg.dggcloud.service.jrcrm.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import net.dgg.dggcloud.core.util.DateUtil;
import net.dgg.dggcloud.service.common.base.SessionManager;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.iboss.constant.SysRecordConstant;
import net.dgg.dggcloud.service.jrcrm.service.log.SysOpreateModuleRecordService;
import net.dgg.dggcloud.service.service.MenuService;
import net.dgg.framework.tac.mq.rabbitmq.queue.DggMessageSendUtil;
import net.dgg.framework.tac.utils.idutil.DggKeyWorker;

/**
 * 操作模块Filter
 * 
 * @Title: OpreateModuleFilter
 * @Description:
 * @Auther: 雷力
 * @Date: 2018-11-09 14:48:54
 *
 */
@Component
public class OpreateModuleFilter implements Filter {

	private Logger logger = Logger.getLogger(getClass());

	@Autowired
	private MenuService menuService;

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = ((HttpServletResponse) res);
		try {
			String uri = request.getRequestURI();
			String url = request.getRequestURL().toString();
			User user = SessionManager.getCurrentUser();
			String queryurl = request.getQueryString();
			
			List<String> uris = new ArrayList<>();
			uris.add(uri);
			// 拼接参数
			if (null != queryurl) {
				String[] params = queryurl.split("&");
				for (int i = 0; i < params.length; i++) {
					if (i == 0) {
						uris.add(uri += "?" + queryurl.split("&")[i]);
					} else {
						uris.add(uri += queryurl.split("&")[i]);
					}
					url += "?" + queryurl.split("&")[i];
				}
			}
			List<String> menus = menuService.findAllUrlsByCache();
			menus.retainAll(uris);
			if (menus.isEmpty()) {
				chain.doFilter(request, response);
				return;
			}
			Map<String, Object> record = new HashMap<>();
			record.put("id", DggKeyWorker.nextId());
			record.put("opreateTime", DateUtil.dateToString(new Date()));
			record.put("uri", menus.get(0));
			record.put("url", url);
			record.put("userLoginname", user.getLoginName());
			record.put("userName", user.getName());
			record.put("clazz", SysOpreateModuleRecordService.class);
			logger.info("======================>发送消息...");
			DggMessageSendUtil.putMessage(SysRecordConstant.QUEUENAME, record);
		} catch (Exception e) {
			e.printStackTrace();
		}
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {

	}

}
